import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * @author Shasha Chen
 */

public class MyTank extends Tank
{
    private TankWorld tankWorld;
    private MyTankManager mtm;
    //Bullet Direction
    private int bd;      
    private int xPos;
    private int yPos;
    

    
    public MyTank()
    {
       super();
       mtm = new MyTankManager();
        

    }
    public void act() 
    {
        // Add your action code here.
        
        CheckKey();
        //explode   
        Actor a = getOneIntersectingObject(EnemyBullet.class);
        if (a != null)
        {
            mtm.GetHit();
            getWorld().removeObject(a);
            if(mtm.getState() == "DeadState")
            {
                myTankExplode();
            }
        } 
    }   
        
    
    private void CheckKey(){
         if (Greenfoot.isKeyDown("left"))
        {
            turn(-2);
            bd = bd - 2;
           // move(1);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn(2);
            bd = bd + 2;
            //move(1);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            move(2);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            move(-2);
        }
        //if (Greenfoot.isKeyDown("space"))
        if (Greenfoot.getKey() == "space")
        {
            fire();
        }
        
        xPos = this.getX();
        yPos = this.getY();
       
    }
    
    /*
    private void fire()
    {
       Bullet b = new Bullet();
       b.turn(bd);
       getWorld().addObject(b,this.getX(), this.getY());
       
    }
    */
    
    private void fire()
    {
        
        Bullet bullet = new Bullet();
        getWorld().addObject(bullet, getX() + 2, getY() - 2);
        bullet.setRotation(getRotation());
        
    }
    
    private void myTankExplode()
    {
        
        getWorld().addObject(new Explosion(),xPos,yPos);
        getWorld().removeObject(this);
        
    }
    
}

    













